package com.shzg.im.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;

import com.shzg.im.controller.UserController;

public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        if (request.getRequestURI().equals("/shzg_im/")) {
            return true;
        } else if (request.getRequestURI().contains("user/login") == false
                && request.getSession().getAttribute("user") == null) {
            String url = MvcUriComponentsBuilder.fromMethodName(UserController.class, "login").build().toString();
            System.out.println("Send to : " + url);
            response.sendRedirect(url);
        } else {
            return true;
        }

        return false;

    }

}
